package Vista;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import Controlador.Sistema;
import Negocio.LoteCompuestoView;
import Negocio.PropietarioView;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModificarLoteCompuesto extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton jButton1;
	private JButton botonBuscar;
	private JMenu jMenu1;
	private JLabel jLabel2;
	private JTextField fieldCodigo;
	private JLabel jLabel1;
	private JLabel jLabel6;
	private JButton agregar;
	private JButton jButton2;
	private Vector <PropietarioView> prop;
	private JComboBox propiet;
	private Vector <Long> lotes;
	private JComboBox lot;
	private ArrayList <Long> l ;
	private LoteCompuestoView ls;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ModificarLoteCompuesto inst = new ModificarLoteCompuesto();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ModificarLoteCompuesto() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Modificar Lote Compuesto");
			l = new ArrayList <Long>();
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Guardar");
				jButton1.setBounds(68, 145, 99, 21);
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("Guardar.actionPerformed, event="+evt);
						int indice = propiet.getSelectedIndex()-1;
						PropietarioView p = prop.elementAt(indice);
						Sistema.getInstancia().ModificarLoteCompuesto(Long.parseLong(fieldCodigo.getText()),l,p.getDni());
						dispose();
					}
				});
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("Cancelar");
				jButton2.setBounds(190, 145, 92, 21);
			}
			{
				agregar = new JButton();
				agregar.setEnabled(false);
				getContentPane().add(agregar);
				agregar.setText("Agregar");
				agregar.setBounds(247, 101, 85, 21);
				agregar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jButton3.actionPerformed, event="+evt);
						int indice = lot.getSelectedIndex()-1;
						l .add(lotes.elementAt(indice));
					}
				});
			}
			{
				jLabel6 = new JLabel();
				getContentPane().add(jLabel6);
				jLabel6.setText("Lotes");
				jLabel6.setBounds(49, 104, 38, 14);
			}
			{
				jLabel1 = new JLabel();
				jLabel1.setText("Propiertario");
				jLabel1.setBounds(12, 74, 81, 14);
			}
			{
				jMenu1 = new JMenu();
				getContentPane().add(jMenu1);
				getContentPane().add(jLabel1);
				jMenu1.setText("jMenu1");
			}
			{
				prop = new Vector <PropietarioView>();
				prop = Sistema.getInstancia().buscarPropietarios();
				//Armar texto
				Vector<String> nombres = new Vector<String>();
				nombres.add("  ");
				for (int i=0;i<prop.size();i++)
				{
					nombres.add(prop.elementAt(i).getNombre());
				}
				ComboBoxModel MineralesModel = new DefaultComboBoxModel(nombres);
				propiet = new JComboBox();
				getContentPane().add(propiet);
				propiet.setModel(MineralesModel);
				propiet.setBounds(105, 71, 112, 21);
				propiet.setEnabled(false);
			}
			{
				lotes = new Vector <Long>();
				lotes = Sistema.getInstancia().buscarLotes();
				
				
				
				lot = new JComboBox();
				getContentPane().add(lot);
				
				lot.setBounds(105, 101, 112, 21);
				lot.setEnabled(false);
			}
			{
				botonBuscar = new JButton();
				getContentPane().add(botonBuscar);
				botonBuscar.setText("Buscar");
				botonBuscar.setBounds(290, 19, 90, 23);
				botonBuscar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("botonBuscar.actionPerformed, event="+evt);
						long cod = Long.parseLong(fieldCodigo.getText());
						ls = Sistema.getInstancia().buscarLoteCompuesto(cod);
						if (ls != null)
						{
							setPanel();
							fieldCodigo.setEnabled(false);
							botonBuscar.setEnabled(false);
							propiet.setEnabled(true);
							lot.setEnabled(true);
							agregar.setEnabled(true);
							
							
						}
						else
						{
							System.out.println("NO EXISTE EL LOTE");
						}
					}
				});
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Nro Lote:");
				jLabel2.setBounds(18, 24, 79, 14);
			}
			{
				fieldCodigo = new JTextField();
				getContentPane().add(fieldCodigo);
				fieldCodigo.setBounds(127, 20, 139, 21);
			}

			pack();
			this.setSize(503, 304);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void setPanel(){
		lotes.remove(Long.parseLong(fieldCodigo.getText()));
		//Armar texto
		Vector<String> nombres = new Vector<String>();
		nombres.add("  ");
		
		for (int i=0;i<lotes.size();i++)
		{
			nombres.add(String.valueOf(lotes.elementAt(i)));
		}
		ComboBoxModel MineralesModel = new DefaultComboBoxModel(nombres);
		lot.setModel(MineralesModel);
	}
	


}
